home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 469 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.rrz.uni-hamburg.de!rzdspc1!simmons
  2. From: simmons@rzdspc1.informatik.uni-hamburg.de (Geoffrey Simmons)
  3. Newsgroups: comp.lang.c,gnu.gcc.help
  4. Subject: Casting unsigned short as unsigned int -> Bus error
  5. Date: 5 Jan 96 15:57:33 GMT
  6. Organization: University of Hamburg -- Germany
  7. Message-ID: <simmons.820857453@rzdspc1>
  8. NNTP-Posting-Host: rzdspc1.informatik.uni-hamburg.de
  9.  
  10. I have a pointer to a struct with a member declared as unsigned short:
  11.  
  12.     typedef struct {
  13.         ...
  14.         unsigned short myshort;
  15.         ...
  16.         } RecType;
  17.  
  18.     RecType *MyRec;
  19.  
  20. Now when I try to dereference that member, casting it to (unsigned int),
  21. I get a bus error:
  22.  
  23.     unsigned int myint;
  24.  
  25.     myint = (unsigned int) MyRec->myshort;    /* Bus error! */
  26.  
  27. I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
  28. that the pointer points to valid data.
  29.  
  30. Actually, I'd like to print the value using printf, but it crashes on printf
  31. with a bus error. Since printf always promotes shorts to ints, and since
  32.  
  33.     printf("%d\n", MyRec->myshort);
  34.  
  35. produces a bus error like the code shown above, I assume that printf is
  36. crashing for the same reason that the code shown above crashes (whatever
  37. that may be). (It also crashes if I use printf("%hu\n", ...), or if I cast
  38. the argument of printf to (unsigned int) and use "%d\n").
  39.  
  40. The bizarre thing about this is that the debugger (GDB 4.15.1 for
  41. sparc-sun-sunos4.1.4) has no problem dereferencing the struct member I'm
  42. trying to get, for example when I say:
  43.  
  44.     (gdb) printf "%d\n", MyRec->myshort
  45.     1000
  46.  
  47. where 1000 is precisely the value I expect to see. Using the debugger, I
  48. can detect nothing that explains what's wrong; the data's all there, and
  49. the program can dereference the other members of the struct just fine. I'm
  50. quite baffled; can anyone seen where I've gone astray?
  51.  
  52. Thanks,
  53. Geoff
  54.  
  55. -- 
  56. Geoffrey Simmons     |  simmons@informatik.uni-hamburg.de    | "Insert wise
  57. University of Hamburg     |    Phone: (++49 40) 54715-381    |  and witty
  58. Vogt-Koelln-Str. 30     |    Fax:   (++49 40) 54715-385    |  quotation
  59. D-22527 Hamburg, Germany |                    |  here."
  60.